package com.note.simpleshop.module.user.controller;

import com.note.simpleshop.constant.ErrorCode;
import com.note.simpleshop.entity.TbAddress;
import com.note.simpleshop.module.user.auth.RequestToken;
import com.note.simpleshop.module.user.auth.UserUtils;
import com.note.simpleshop.module.user.service.AddressService;
import com.note.simpleshop.vo.Msg;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 *@ClaseName:AddressController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/18 0018 23:52
 */
@RestController
@RequestMapping("user/address")
public class AddressController {
    private static final Logger logger = LoggerFactory.getLogger(AddressController.class);
    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "list", notes = "用户地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "list")
    @RequestToken
    public Msg list() {
        String currentUserId = UserUtils.currentUserId();
        Msg msg = new Msg();
        try {
            List<TbAddress> list = addressService.findByUserId(currentUserId);
            msg.setErrorCode(ErrorCode.ERROR_0000);
            msg.setData(list);
        } catch (Exception e) {
            logger.info(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }

    @ApiOperation(value = "update", notes = "添加用户地址")
    @ApiParam(hidden = true,name ="id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "add")
    @RequestToken
    public Msg addAddress(TbAddress address) {
        String currentUserId = UserUtils.currentUserId();
        Msg msg = new Msg();
        try {
            address.setUserId(currentUserId);
            String addressId = addressService.save(address);
            msg.setErrorCode(ErrorCode.ERROR_0000);
            msg.setData(addressId);
        } catch (Exception e) {
            logger.info(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }

    @ApiOperation(value = "update", notes = "更新用户地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "update")
    @RequestToken
    public Msg updateAddress(TbAddress address) {
        String currentUserId = UserUtils.currentUserId();
        Msg msg = new Msg();
        try {
            if (StringUtils.isBlank(address.getId())) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("id不为空");
            } else {
                address.setUserId(currentUserId);
                addressService.update(address);
                msg.setErrorCode(ErrorCode.ERROR_0000);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @ApiOperation(value = "setDefaul", notes = "设置默认收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "setDefaul")
    @RequestToken
    public Msg setDefault(String addressId) {
        String currentUserId = UserUtils.currentUserId();
        Msg msg = new Msg();
        try {
            if (StringUtils.isBlank(addressId)) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("addressId不为空");
            } else {
                addressService.updateIsUse(addressId,currentUserId,TbAddress.IS_USE1);
                msg.setErrorCode(ErrorCode.ERROR_0000);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @ApiOperation(value = "delete", notes = "删除收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "delete")
    @RequestToken
    public Msg delete(String addressId) {
        String currentUserId = UserUtils.currentUserId();
        Msg msg = new Msg();
        try {
            if (StringUtils.isBlank(addressId)) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("addressId不为空");
            } else {
                addressService.delete(addressId,currentUserId);
                msg.setErrorCode(ErrorCode.ERROR_0000);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
}
